Passed
Push — master ( 1a93c4...2b2dc1 )
by Andrii
02:47
created

test-runner.ts ➔ rfs   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
1
import {readFileSync} from 'fs'
2
import type { Options } from './src/options.types'
3
import postcss7 = require("postcss")
4
import creator7 = require("./src/7")
5
import postcss8 from 'postcss8'
6
import creator8 = require("./src")
7
import type Processor from 'postcss8/lib/processor'
8
9
export type RunOpts = Partial<{
10
  from: string
11
  input: string
12
  outputPath: string | false
13
  errorsCount: number
14
}>
15
16
const {parse: $parse} = JSON
17
, launch = (opts?: Options) => {
18
  const launchers = [postcss7([creator7(opts)]), postcss8([creator8(opts)])]
19
  return (runOpts: RunOpts) => run(launchers, runOpts)
20
}
21
22
export default launch
23
24
export {
25
  rfs, rfsl, readOpts, suiteName
26
}
27
28
async function run(launchers: (postcss7.Processor | Processor)[], runOpts: RunOpts) {
29
  const {
30
    errorsCount = 0,
31
    from,
32
    input = from && rfs(from),
33
  } = runOpts
34
  if (!input)
35
    throw Error("no test input")
36
37
  for (let i = 0; i < launchers.length; i++) {
38
    const result = await launchers[i].process(input, { from })
39
    , { outputPath: output } = runOpts
40
41
    expect(result.warnings()).toHaveLength(errorsCount)
42
43
    output && expect(rfsl(`${from}.d.ts`)).toStrictEqual(rfsl(output))
44
  }
45
}
46
47
function rfs(path: string) {
48
  return readFileSync(path).toString()
49
}
50
51
function rfsl(path: string, eol = "\n") {
52
  return rfs(path).split(eol)
53
}
54
55
function readOpts(path: string) {
56
  return $parse(rfs(path)) as Options
57
}
58
59
function suiteName(path: string) {
60
  return path
61
  .replace(/^.*[\/\\]/, '')
62
  .replace(/\..*$/, '')
63
}
64